package it.univaq.di.helpdesk.presentation;

import it.univaq.di.helpdesk.business.common.FaqService;
import it.univaq.di.helpdesk.business.common.HelpDeskServiceFactory;
import it.univaq.di.helpdesk.business.common.OfficeService;
import it.univaq.di.helpdesk.business.common.TicketService;
import it.univaq.di.helpdesk.business.common.UserService;
import it.univaq.di.helpdesk.business.errors.IntegrityConstraintException;
import it.univaq.di.helpdesk.business.model.Category;
import it.univaq.di.helpdesk.business.model.Faq;
import it.univaq.di.helpdesk.business.model.Office;
import it.univaq.di.helpdesk.business.model.User;
import it.univaq.di.helpdesk.common.utils.AjaxResponse;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class ManageFaqAction extends MappingDispatchAction {
	
	/* AJAX Request */
	public ActionForward viewfaqhistory(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		TicketService service = factory.getTicketService();
		UserService serviceUser = factory.getUserService();
	
		List<Category> category = service.findParentCategories();
		request.setAttribute("category", category);
		
		List<User> operators = serviceUser.findAllTechnics();
		request.setAttribute("operators", operators);
		
		return mapping.getInputForward();
	}
	
	/* AJAX Request */
	public ActionForward getfaqlist(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		
		FaqHistoryForm form = (FaqHistoryForm) actionForm;
		
		Map<String, String> queryParam = new HashMap<String, String>();
		queryParam.put("contents", form.getContents());
		queryParam.put("category", form.getCatID());
		queryParam.put("subcategory", form.getSubCategories());
		queryParam.put("userid", form.getUserID());
		
		List<Faq> faqs = service.searchForFaqs(queryParam);
		request.setAttribute("faqs", faqs);
		
		return mapping.findForward("ticket.getfaqlist");
	}
	
	
	/* AJAX Request */
	public ActionForward findfaqsbycategory(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		
		Map<String, String> queryParam = new HashMap<String, String>();
		queryParam.put("contents", null);
		queryParam.put("category", request.getParameter("category"));
		queryParam.put("subcategory", request.getParameter("subcatID"));
		
		List<Faq> faqs = service.searchForFaqs(queryParam);
		request.setAttribute("faqs", faqs);
		
		return mapping.findForward("ticket.searchforfaqs");
	}
	
	/* AJAX Request */
	public ActionForward loadfaq(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		
		int faqID = Integer.parseInt(request.getParameter("itemID"));
		
		Faq faq = service.findFaqById(faqID);
		
		request.setAttribute("item", faq);
		
		return mapping.findForward("ticket.faqbox");
	}
	
	
	public ActionForward activeaddfaq(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();		
		TicketService service = factory.getTicketService();
		
		List<Category> category = service.findParentCategories();
		
		request.setAttribute("category", category);
		
		// Salvataggio del token riutilizzato nel metodo addticket.
		saveToken(request);
			
		return mapping.findForward("operator.activeaddfaq");
	}
	
	/* AJAX Request */
	public ActionForward addfaq (ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		/* controlla se il token inserito in sessione dal metodo activeaddticket
		 * è valido rispetto a quello inviato con il submit del form e lo elimina 
		 * dopo l'inserimento 
		 */
		if (!isTokenValid(request, false)) {
			return mapping.findForward("common.doublesubmit");
		}
		
		Faq faq = new Faq();
		FaqForm faqForm = (FaqForm) actionForm;
		
		BeanUtils.copyProperties(faq, faqForm);
		faq.setContent(faqForm.getFaqcontent());
		
		User user = (User) request.getSession().getAttribute("user");
		faq.setUser(user);
		faq.setFaqDate(new Date());
		
		Category subCat = new Category();
		subCat.setCategoryID(faqForm.getSubCategoryID());
		faq.setCategory(subCat);
		
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		
		service.addFaq(faq);
		
		// Rimozione del token dalla sessione
		resetToken(request);
		
		return mapping.findForward("common.ok");
	}
	
	public ActionForward activeeditfaq(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		FaqForm faqForm = (FaqForm) actionForm;
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		TicketService serviceCategory = factory.getTicketService();
		
		//recupero l'ID della faq corrente
		int faqID = Integer.parseInt(request.getParameter("faqID"));
		Faq faq = service.getFaqById(faqID);
		
		//copio il contenuto di faq dentro faqForm
		BeanUtils.copyProperties(faqForm, faq);
		faqForm.setFaqcontent(faq.getContent());
		
		faqForm.setCatID(faq.getCategory().getParent().getCategoryID());
		faqForm.setSubCategoryID(faq.getCategory().getCategoryID());
		
		List<Category> categories = serviceCategory.findParentCategories();
		request.setAttribute("category", categories);
		
		// Salvataggio del token riutilizzato nel metodo editfaq.
		saveToken(request);
		
		return mapping.findForward("operator.activeeditfaq");
	}
	
	/* AJAX Request */
	public ActionForward editfaq(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		/* controlla se il token inserito in sessione dal metodo activeaddoffice
		 * è valido rispetto a quello inviato con il submit del form e lo elimina 
		 * dopo l'inserimento 
		 */
		if (!isTokenValid(request, false)) {
			return mapping.findForward("common.doublesubmit");
		}	
		
		Faq faq = new Faq();
		FaqForm faqForm = (FaqForm)actionForm;
		BeanUtils.copyProperties(faq, faqForm);
		faq.setContent(faqForm.getFaqcontent());
		
		Category category = new Category();
		category.setCategoryID(faqForm.getSubCategoryID());
		faq.setCategory(category);
		
		User user = (User) request.getSession().getAttribute("user");
		faq.setUser(user);
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		
		service.updateFaq(faq);
		
		// Rimozione del token dalla sessione
		resetToken(request);
		
		return mapping.findForward("common.ok");
	}
	
	/* AJAX Request */
	public ActionForward deletefaq(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		FaqService service = factory.getFaqService();
		
		int faqID = Integer.parseInt(request.getParameter("faqID"));
		
		service.deleteFaq(faqID);
		
		return null;
	}
}
